<?php
	require_once "includes/pagination.php";
	require_once 'includes/class.phpmailer.php';

	$itemPerPage=10;
	if($_SESSION['user_id_sess']=="") {
		echo "<script type='text/javascript'> window.location='index.php?signin&url=mylist'; </script>";
	}
	$userId = $_SESSION['user_id_sess'];
?>
<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>My Listings</h2></td>
  </tr>
  <tr>
    <td>		
      	 
		 <?php
		 	//delete single product		 
			if(!empty($_POST['hidDelPro'])) {
			
				$proId=$_POST['hidDelPro'];
				$sqlDelPro="Delete from tbl_product where product_id=$proId";
				$rstDelPro=mysql_query($sqlDelPro);
				if($rstDelPro) { 
				//setup for auto email
					$sqlEmail="select * from tbl_emails where email_type='Item Deleted'";
					$resEmail=mysql_query($sqlEmail);
					$rowEmail=mysql_fetch_array($resEmail);
					
					$sqlSellers="select * from tbl_items ti, tbl_users tu where tu.user_id=ti.seller_id and ti.product_id='".$proId."'";
					$resSellers=mysql_query($sqlSellers);
					while($rowSellers=mysql_fetch_array($resSellers))
					{
					$subj=str_replace('##_ItemName',$_POST['itemname'],$rowEmail['email_subject']);
					$emailContent=str_replace('##_MerchantName',$rowSellers['username'],$rowEmail['email_body']);
					$emailContent=str_replace('##_ItemName',$_POST['itemname'],$emailContent);
	  $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
  <tr>
    <td width='18%' height='41'><table width='100%' border='0'>
      <tr>
        <td width='19%' height='68'><img src='http://www.swapen.com/images/logo.gif'></td>
        <td width='81%'> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = $subj;
		$mail->AddAddress($rowSellers['email'], $firstName);
		$mail->Send();
					}
					$imgPaths=getImagePath($proId);
					if($imgPaths[0]!="") { unlink($imgPaths[0]); }
					if($imgPaths[1]!="") { unlink($imgPaths[1]); }
					if($imgPaths[2]!="") { unlink($imgPaths[2]); }
					echo "<div id='suc-msg'> Your product deleted. </div><br>"; 
				}	
				else {
					echo "<div id='err-msg'> Your product could not be deleted. </div><br>";
				}
			}			
			   
		 ?>
		 <form name="frmProlist" id="frmProlist" method="post" action="">
		 	<input type="hidden" name="hidDelPro" id="hidDelPro">
		 <?php		   
				   $sqlSelPro="SELECT * FROM tbl_product where posted_by='$userId' ORDER BY product_id DESC";
				   $rstSelPro=mysql_query(getPagingQuery($sqlSelPro,$itemPerPage));				  
				   $num=mysql_num_rows($rstSelPro);
				   
				   if($num==0)
					{
					  echo "<div style='border-top:1px solid #CCCCCC;'> </div><br>";
					  echo "<p style='padding:5px;'>No items found on your list...</p><br>";
					  echo "<div style='border-top:1px solid #CCCCCC;'> </div><br>";
					}
				   else {	?>
				   		  <br><table width='100%' border='0' align='center' cellspacing='0'>
						  <tr class='head-tr'>
								  <td width="6%"><strong>S.No</strong></td>								
								  <td width="34%"> <strong>Product name </strong></td>
								  <td width="13%"> <strong> -- </strong></td>
								  <td width="14%"> <strong>Price</strong> </td>
							      <td width="20%"><strong>Time Left</strong> </td>
								  <td width="6%">&nbsp; </td>
								  <td width="7%">&nbsp; </td>
								</tr>				 
						 <?php
						 $r=1;
						 while($rowSelPro=mysql_fetch_array($rstSelPro))
					     {						   
						   $productId=$rowSelPro['product_id'];
						   $productName=wordFormater($rowSelPro['product_name'],20);
						   $productTitle = $rowSelPro['product_name'];					   
						   $productPrice=$rowSelPro['product_price'];
						   $productThumb=$rowSelPro['product_thumbnail'];
						   $productImage=$rowSelPro['product_image'];
						   $postedDate=$rowSelPro['posted_date'];
						   $bargainDate = $rowSelPro['bargain_duration'];
						   $timeLeft = getRemainingTime($bargainDate);
						   if($timeLeft < 0) {
						   		$timeLeft = "<font color='red'>Listing Expired</font>";
						    }
						  
						   					   
						   echo "<tr>
						   		 <td height='28'> $r </td>							         
								 <td > <a href='index.php?products=$productId' title='$productTitle'> $productName </a> </td>
								 <td > <input type='image' src='images/buy.gif' style='cursor:pointer;' onClick=''> </td>
								 <td > $$productPrice </td>
								 <td > $timeLeft </td>
						      	 <td><a href='index.php?editlist=$productId'>Edit</a></td>
								 <td><a href='javascript:void(0);' onClick='funDelProduct('frmProlist','prod',$productId)'>Delete</a></td>
							   </tr>";	
							echo "<tr>
									 <td colspan='7'> <div style='border-top:1px solid #CCCCCC;'> </div> </td>
								  </tr>";   			  
						 	$r++;
						 } ?>
						</table>				    	
						<?php
					} 				
	?>
	
	</form>
	</td>
</tr>	

<tr>
	<td><div align="center"><?php
		   $link="mylist";
		   echo $disp=getPagingLink($sqlSelPro,$itemPerPage,$link);							   
	 ?></div>
	</td>
</tr>
</table>
</div>	

							
	